<?php

namespace MoChat\App\Sales\Service;

use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\App\Sales\Model\SalesTask;
use MoChat\Framework\Service\AbstractService;

class SalesTaskService extends AbstractService implements SalesTaskContract
{
    /**
     * @var SalesTask
     */
    protected $model;

    public function createSalesTask($data): int
    {
        return $this->model->createOne($data);
    }

    public function getSalesTaskList($where, array $filed = ['*']): array
    {
        return $this->model->getPageList($where, $filed);
    }

    public function updateSalesTaskById($id, $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    public function showSalesTaskById($id): array
    {
        return $this->model->getOneById($id);
    }

    public function getSalesTaskByIdLock($id)
    {
        return $this->model->lockForUpdate()->select(['*'])->find($id);
    }

    public function updateAllocatedSalesTask($id, $number)
    {
        return $this->model->where('id', $id)->increment('allocated_sales_task', $number);
    }

    public function deleteSalesTask($id): int
    {
       return $this->model->deleteOne($id);
    }

    /**
     * 获取销量指标总额
     * @param $corpId
     * @param $startTime
     * @param $endTime
     * @return int|mixed|string|void
     */
    public function getTotalSales($corpId,$startTime,$endTime)
    {
       return $this->model->where('corp_id',$corpId)
           ->where('created_at','>=',$startTime)
           ->where('created_at','<',$endTime)
           ->sum('sales_task');
    }
}